package org.example.questionnairebackend.controller;

import org.example.questionnairebackend.Data.Submissions;
import org.example.questionnairebackend.Data.Surveys;
import org.example.questionnairebackend.Dto.QuestionDTO;
import org.example.questionnairebackend.Vo.AddVo;
import org.example.questionnairebackend.Vo.SurveyCreateRequest;
import org.example.questionnairebackend.result.Result;
import org.example.questionnairebackend.service.SurveysService;
import org.example.questionnairebackend.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/surveys")
public class SurveysController {
    @Autowired
    private SurveysService surveysService;

    //获取所有调查问卷
    @RequestMapping("/getAllSurveys")
    private Result<List<Surveys>> getAllSurveys() {
        List<Surveys> list = surveysService.getAllSurveys();
        return Result.success(list);
    }

    @GetMapping("/{surveyId}/questions")
    public Result<List<QuestionDTO>> getSurveyQuestions(@PathVariable Integer surveyId) {
        List<QuestionDTO> questions = surveysService.getSurveyQuestions(surveyId);
        return Result.success(questions);
    }

    //问卷提交
    @PostMapping("/submit/{surveyId}")
    public Result submitSurvey(@PathVariable Integer surveyId) {
        Submissions submissions = new Submissions();
        submissions.setSurveyId(surveyId);
        submissions.setUserId(BaseContext.getCurrentId());
        submissions.setIpAddress("10.0.2.2");
        submissions.setSubmitTime(new java.util.Date());
        surveysService.submitSurvey(submissions);
        return Result.success();
    }

    //添加问卷
    @PostMapping("/addSurvey")
    public Result<Integer> addSurvey(@RequestBody Surveys surveys) {
        surveys.setUserId(BaseContext.getCurrentId());
        surveys.setCreatedAt(new java.util.Date());
        surveys.setUpdatedAt(new java.util.Date());
        surveys.setStatus(1);
        Integer added = surveysService.addSurvey(surveys);
        return Result.success(added);
    }

    //添加问卷题目
    @PostMapping("/add")
    public Result createSurvey(@RequestBody SurveyCreateRequest request) {
        surveysService.createSurvey(request);
        return Result.success();
    }

}
